from django.http import JsonResponse
from django.shortcuts import render, redirect
from app01.models import *
from app01.form.bookbase import *
# Create your views here.


def add_publisher(request):

    print('1')
    if request.method == "POST":
        print(request.POST)
        form=App01ModleForm(request, data=request.POST)
        if form.is_valid():
            instance = form.save()
            return JsonResponse({'status': True, 'data': '/app01/publisher_list/'})
        print(form.errors)
        return JsonResponse({'status': False, 'error': form.errors})

        # # 获取表单提交内容
        # publisher_name = request.POST.get("name")
        # publisher_address = request.POST.get("address")
        # # 保存到数据库
        # models.Publisher.objects.create(name=publisher_name, address=publisher_address)
    form = App01ModleForm(request)
    return render(request,'add_publisher.html',{'form':form})

def publish_list(request):
    # 查询数据库中的所有信息
    publisher_list = Publisher.objects.all()
    return render(request, "publisher_list.html", {"publisher_obj_list": publisher_list})


def edit_publisher(request):
    if request.method == 'POST':
        # 1获取表单提交过来的内容
        id = request.POST.get('id')
        name = request.POST.get('name')
        address = request.POST.get('address')
        # 2根据id 去数据库查找对象
        publisher_obj = Publisher.objects.get(id=id)
        # 3修改
        publisher_obj.name = name
        publisher_obj.address = address
        publisher_obj.save()
        # 4重定向到出版社列表
        return redirect('/app01/publisher_list/')
    else:
        # 1获取id
        id = request.GET.get('id')
        # 2去数据库中查找相应的数据
        publisher_obj = Publisher.objects.get(id=id)
        publisher_obj_list = Publisher.objects.all()
        # 3返回页面
        return render(request, 'edit_publisher.html',
                      {"publisher_obj": publisher_obj, "publisher_obj_list": publisher_obj_list})


def delete_publisher(request):
    # 获取要删除的id
    id = request.GET.get("id")
    # 根据id删除数据库中的记录
    Publisher.objects.filter(id=id).delete()
    return redirect("/app01/publisher_list")


def book_list(request):
    # 获取图书信息
    book_obj_list = Book.objects.all()
    # 将数据放到页面上
    return render(request, 'book_list.html', {"book_obj_list": book_obj_list})


def add_book(request):
    print("here")
    if request.method == 'POST':
        form = BookModleForm(request, data=request.POST)
        if form.is_valid():
            #instance = form.save()
            # 获取表单提交过来的内容
            name = request.POST.get('name')
            price = request.POST.get('price')
            inventory = request.POST.get('inventory')
            sale_num = request.POST.get('sale_num')
            publisher_id = request.POST.get('publisher_id')
            # 保存到数据库app01_book
            Book.objects.create(name=name, price=price,
                                       inventory=inventory, sale_num=sale_num,publisher_id=publisher_id)
            return JsonResponse({'status': True, 'data': '/app01/book_list/'})
        print(form.errors)
        return JsonResponse({'status': False, 'error': form.errors})
        # # 获取表单提交过来的内容
        # name = request.POST.get('name')
        # price = request.POST.get('price')
        # inventory = request.POST.get('inventory')
        # sale_num = request.POST.get('sale_num')
        # publisher_id = request.POST.get('publisher_id')
        # # 保存到数据库app01_book
        # Book.objects.create(name=name, price=price,
        #                            inventory=inventory, sale_num=sale_num,publisher_id=publisher_id)
        # # 重定向到图书列表页面
        # return redirect('/app01/book_list/')
    else:
        # 获取所有出版社
        form = BookModleForm(request)
        publisher_obj_list = Publisher.objects.all()
        return render(request, "add_book.html", {"publisher_obj_list": publisher_obj_list,"form":form})


def edit_book(request):
    if request.method == 'GET':
        # 获取id
        id = request.GET.get('id')
        # 取数据库中查找相应数据
        book_obj = Book.objects.filter(id=id).first()
        # 查找所有的出版社
        publisher_list = Publisher.objects.all()
        # 返回页面
        return render(request, 'edit_book.html', {'book_obj': book_obj, 'publisher_list': publisher_list})
    else:
        # 获取表单提交过来的内容
        id = request.POST.get('id')
        name = request.POST.get('name')
        inventory = request.POST.get("inventory")
        price = request.POST.get("price")
        sale_num = request.POST.get("sale_num")
        publisher_id = request.POST.get('publisher_id')
        # 查询数据库进行更新
        Book.objects.filter(id=id).update(name=name, inventory=inventory, price=price, sale_num=sale_num,
                                                 publisher_id=publisher_id)
        # 重定向到boo_list
        return redirect("/app01/book_list")


def delete_book(request):
    # 获取id
    id = request.GET.get("id")
    #  删除图书
    Book.objects.filter(id=id).delete()
    # 重定向到图书列表
    return redirect("/app01/book_list")
